<template>
  <div class="main" style="margin-top:80px;">
    <a-form-model layout="horizontal" ref="ruleForm" :rules="rules" :model="form" v-bind="{labelCol: { span: 4 }, wrapperCol: { span: 20, offset: 2 }}">
      <a-form-model-item prop="username">
        <a-input v-model="form.username" placeholder="请输入用户名">
          <a-icon slot="prefix" type="user" :style="{ color: 'rgba(0,0,0,.25)' }" />
        </a-input>
      </a-form-model-item>
      <a-form-model-item prop="password">
        <a-input-password v-model="form.password" placeholder="请输入密码" @keyup.enter="handleSubmit">
          <a-icon slot="prefix" type="lock" :style="{ color: 'rgba(0,0,0,.25)' }" />
        </a-input-password>
      </a-form-model-item>
      <a-form-model-item>
        <a-button
          type="primary"
          block
          :loading="loginBtn"
          :disabled="loginBtn"
          @click="handleSubmit"
        >
          登录
        </a-button>
      </a-form-model-item>
    </a-form-model>
  </div>
</template>

<script>
import { mapActions } from 'vuex'
import { timeFix } from '@/utils/util'
export default {
  data () {
    return {
      form: {},
      loginBtn: false,
      rules: {
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
          { max: 20, min: 4, message: '用户名的长度为4-20位', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          // { pattern: '^.*(?=.*[0-9])(?=.*[A-Z])(?=.*[a-z])(?=.*[!@#$%^&*?])', message: '密码必须包含数字、大小写字母和特殊符号', trigger: 'blur' }
        ]
      }
    }
  },
  created () {},
  methods: {
    ...mapActions(['Login']),
    handleSubmit (e) {
      this.$refs.ruleForm.validate(valid => {
        if (valid) {
          this.loginBtn = true
          const data = {
            username: this.form.username,
            password: window.btoa(this.form.password)
            // username: 'admin',// this.form.username,
            // password: 'Wjl4OGM3djYh'//base64(this.form.password)

          };
          this.$store.dispatch('Login', data)
            .then((res) => {
              this.loginSuccess(res)
            })
            .catch(err => {
              this.requestFailed(err)
            })
            .finally(() => {
              this.loginBtn = false
            })
        } else {
          setTimeout(() => {
            this.loginBtn = false
          }, 1000)
        }
      });
    },
    // 登录成功
    loginSuccess (res) {
      this.$router.push({ path: '/' });
      this.$notification.success({
        message: '欢迎',
        description: `${timeFix()}，欢迎回来`
      })
    },
    // 登录失败
    requestFailed (err) {
      this.$notification['error']({
        message: '错误',
        description: err.msg || '请求出现错误，请稍后再试',
        duration: 4
      })
    }
  }
}
</script>

<style lang="less" scoped>
.user-layout-login {
  label {
    font-size: 14px;
  }

  .forge-password {
    font-size: 14px;
  }

  button.login-button {
    padding: 0 15px;
    font-size: 16px;
    height: 40px;
    width: 100%;
  }

  .user-login-other {
    text-align: left;
    margin-top: 24px;
    line-height: 22px;

    .item-icon {
      font-size: 24px;
      color: rgba(0, 0, 0, 0.2);
      margin-left: 16px;
      vertical-align: middle;
      cursor: pointer;
      transition: color 0.3s;

      &:hover {
        color: #1890ff;
      }
    }

    .register {
      float: right;
    }
  }
}
</style>
